package com.neu.admin;

import com.neu.service.ShoppingIssueService;
import com.neu.util.response.BaseResponse;
import com.neu.vo.EmptyVo;
import com.neu.vo.ShoppingBrand;
import com.neu.vo.ShoppingIssue;
import com.neu.vo.fenye;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by yinjian by 2019/11/22
 * 常见问题 尹健
 */

@RestController
@RequestMapping("/admin/issue")
public class AdminIssueController {

    @Autowired
    private ShoppingIssueService shoppingIssueService;

    /* *
     * 作用:查看常见问题
        {
            "pagenum":1,
            "pagesize":2,
            "question":"如"
        }
     * yinjian on 2019/11/22
     * @param shoppingIssue
     **/
    @ApiOperation(value= "查看常见问题")
    @PostMapping("/list")
    private ResponseEntity<BaseResponse<EmptyVo>> list(@RequestBody ShoppingIssue shoppingIssue){
        if (null == shoppingIssue.getPagenum() || "".equals(shoppingIssue.getPagenum())){
            return BaseResponse.badArgument();
        }
        if (null == shoppingIssue.getPagesize() || "".equals(shoppingIssue.getPagesize())){
            return BaseResponse.badArgument();
        }
        List<ShoppingBrand> list =  shoppingIssueService.querySelective(shoppingIssue);
        fenye fenye = new fenye();
        fenye.setList(list);
        //获取总数
        if (null == shoppingIssue.getQuestion()){
            fenye.setTotal(shoppingIssueService.gettotal());
        }else {
            fenye.setTotal(list.size());
        }
        return BaseResponse.generateOKResponseEntity(fenye);
    }

    /* *
     * 作用:添加常见问题
        {
            "question":"如",
            "answer":"22"
        }
     * yinjian on 2019/11/22
     * @param shoppingIssue
     **/
    @ApiOperation(value = "添加常见问题")
    @PostMapping("/create")
    private ResponseEntity<BaseResponse<EmptyVo>> create(@RequestBody ShoppingIssue shoppingIssue){
        if (null == shoppingIssue.getQuestion() || "".equals(shoppingIssue.getQuestion())){
            return BaseResponse.badArgument();
        }
        if (null == shoppingIssue.getAnswer() || "".equals(shoppingIssue.getAnswer())){
            return BaseResponse.badArgument();
        }
        int code = shoppingIssueService.add(shoppingIssue);
        if (code != 1){
            return BaseResponse.serious();
        }
        return BaseResponse.generateOKResponseEntity("新增成功");
    }

    /* *
     * 作用:修改常见问题
        {
            "id":6,
            "question":"如12",
            "answer":"2221"
        }
     * yinjian on 2019/11/22
     * @param shoppingIssue
     **/
    @ApiOperation(value = "修改常见问题")
    @PostMapping("/update")
    private ResponseEntity<BaseResponse<EmptyVo>> update(@RequestBody ShoppingIssue shoppingIssue){
        if (null == shoppingIssue.getId() || "".equals(shoppingIssue.getId())){
            return BaseResponse.badArgument();
        }
        if (null == shoppingIssue.getQuestion() || "".equals(shoppingIssue.getQuestion())){
            return BaseResponse.badArgument();
        }
        if (null == shoppingIssue.getAnswer() || "".equals(shoppingIssue.getAnswer())){
            return BaseResponse.badArgument();
        }
        int code = shoppingIssueService.updateById(shoppingIssue);
        if (code != 1){
            return BaseResponse.serious();
        }
        return BaseResponse.generateOKResponseEntity("修改成功");
    }

    /* *
     * 作用:删除常见问题
        {
            "id":6
        }
     * yinjian on 2019/11/22
     * @param shoppingIssue
     **/
    @ApiOperation(value = "删除常见问题")
    @PostMapping("/delete")
    private ResponseEntity<BaseResponse<EmptyVo>> delete(@RequestBody ShoppingIssue shoppingIssue){
        if (null == shoppingIssue.getId() || "".equals(shoppingIssue.getId())){
            return BaseResponse.badArgument();
        }
        int code = shoppingIssueService.deleteById(shoppingIssue);
        if (code != 1){
            return BaseResponse.serious();
        }
        return BaseResponse.generateOKResponseEntity("删除成功");
    }

}
